package com.wqr.mobile.android.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.wqr.mobile.android.ui.R;
import com.wqr.mobile.android.ui.api.API;
import com.wqr.mobile.android.ui.widget.HomePagerInterface;
import com.wqr.mobile.android.ui.widget.HomeTitleProvider;

public class WeiboListPagerActivity extends FragmentActivity implements Callback, API {
	String[] mStars = { "杨幂", "舒淇", "刘亦菲", "赵薇" };
	MyAdapter mAdapter;

	ViewPager mPager;

	Handler mHandler;
	HomeTitleProvider mTabs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fragment_pager);
		mPager = (ViewPager) findViewById(R.id.pager);
		mPager.setOffscreenPageLimit(mStars.length);
		mTabs = (HomeTitleProvider) findViewById(R.id.tabs);
		mHandler = new Handler(this);

		mHandler.sendEmptyMessageDelayed(START, 0);

	}

	public class MyAdapter extends FragmentPagerAdapter implements HomePagerInterface {
		public MyAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return mStars.length;
		}

		@Override
		public Fragment getItem(int position) {
			// return new WeboFragment(mStars[position]);
			return new WeboFragmentList(mStars[position]);
		}

		@Override
		public int getItemPosition(Object object) {
			return POSITION_NONE;
		}

		@Override
		public String getTitle(int position) {

			final int len = mStars.length;

			if (position >= 0 && position < len)
				return mStars[position];
			else
				return "";

		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case FINISHED:
			break;
		case START:
			mAdapter = new MyAdapter(getSupportFragmentManager());
			mPager.setAdapter(mAdapter);
			mTabs.setViewPager(mPager);
			break;

		default:
			break;
		}
		return false;
	}
}
